﻿using AutoMapper;
using FreeRedis;
using Lingmeng.EntityFrameworkCore.Core.Base;
using Microsoft.Extensions.Logging.Abstractions.Internal;
using Video.Application.Contract;
using Video.Application.Contract.Base;
using Video.Application.Contract.UserInfos;
using Video.Application.Contract.UserInfos.Dtos;
using Video.Application.Contract.Videos.Dtos;
using Video.Application.Manage;
using Video.Domain;
using Video.Domain.Shared;
using Video.Domain.UserRepository;
using Video.Domain.Users;

namespace Video.Application.UserInfos
{
    public class UserInfoService : IUserInfoService
    {
        private readonly IMapper _mapper; 
        private readonly IUserInfoRepository _userInfoRepository;
        private readonly CurrentManage _currentManage;
        private readonly IUnitOfWork _unitOfWork;
        private readonly RedisClient _redisClient;
        private readonly IRoleRepository _role;
        public UserInfoService(IMapper mapper, IUserInfoRepository userInfoRepository,CurrentManage currentManage,IUnitOfWork unitOfWork,RedisClient redisClient,IRoleRepository role)
        {

            _mapper = mapper;
            _userInfoRepository = userInfoRepository;
            _currentManage = currentManage;
            _unitOfWork = unitOfWork;
            _redisClient = redisClient;
            _role=role;
        }

        

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginInput"></param>
        /// <returns></returns>
        public async Task<UserInfoRoleDto> LoginAsync(LoginInput loginInput)
        {
            var data = await _userInfoRepository.GetUserInfoRoleAsync(loginInput.UserName, loginInput.Password);
            //映射关系的启动器
            var dto =_mapper.Map<UserInfoRoleDto>(data);
            return dto;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public async Task<UserInfoRoleDto> GetAsync()
        {
            var data = await _userInfoRepository.GetAsync(_currentManage.GetId());
            var dto = _mapper.Map<UserInfoRoleDto>(data);
            return dto;
        }
        /// <summary>
        /// 用户修改信息，昵称，密码，头像
        /// </summary>
        /// <param name="updateUserInfo"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public async Task UpdateAsync(UpdateUserInfo updateUserInfo)
        {
            var userinfo= await _userInfoRepository.FirstOfDefaultAsync(x => x.Id == _currentManage.GetId());
            if(userinfo == null)
            {
                throw new BusinessException("用户不存在");
            }
            userinfo.Avatar = updateUserInfo.Avatar;
            userinfo.Password=updateUserInfo.Password;
            userinfo.Name=updateUserInfo.Name;
            await _userInfoRepository.UpdateAsync(userinfo);
            await _unitOfWork.SaveChangeAsync();
        }
        /// <summary>
        /// 用户注册并默认user角色
        /// </summary>
        /// <param name="registerInput"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public async Task<UserInfoRoleDto> RegisterAsync(RegisterInput registerInput)
        {
            var code = await _redisClient.GetAsync<string>($"{CodeType.Register}:{registerInput.UserName}");
            var s = $"{CodeType.Register}:{registerInput.UserName}";
            if (code!=null&&code != registerInput.Code)
            {
                throw new BusinessException("验证码错误");
            }

            if (await _userInfoRepository.IsExistAsync(x => x.UserName == registerInput.UserName))
            {
                throw new BusinessException("用户名已存在");
            }

            var data = _mapper.Map<UserInfo>(registerInput);
            data = await _userInfoRepository.InserAsync(data);

            await _unitOfWork.SaveChangeAsync();
            if (data!=null)
            {
                var role = await _role.GetAsync("user");
                var userRole=new UserRole { RoleId = role.Id ,UserId=data.Id };
                await _role.InserAsync(userRole);
                await _unitOfWork.SaveChangeAsync();
            }
            return _mapper.Map<UserInfoRoleDto>(data);
        }

        public async Task<PageResultDto<UserInfoDto>> GetListAsync(GetListInput getListInput)
        {
            var data= await _userInfoRepository.GetListAsync(
                getListInput.Keywords, getListInput.StartTime, getListInput.EndTime, getListInput.SkipCount, getListInput.MaxResultCount);
            
                
                var dto = _mapper.Map<List<UserInfoDto>>(data);
                var count = await _userInfoRepository.GetCountAsync(getListInput.Keywords, getListInput.StartTime, getListInput.EndTime);
                return new PageResultDto<UserInfoDto>(count, dto);   
        }

        public async Task DeleteAsync(IEnumerable<Guid> guid)
        {
            await _userInfoRepository.DeleteAsync(guid);
        }

        public async Task EnableAsync(IEnumerable<Guid> guid,bool enable=true)
        {
            await _userInfoRepository.EnableAsync(guid,enable);
        }

       
    }
}
